window.onload = function () {
    var btn = document.getElementById("btn");
    var reset = document.getElementById("reset");
    var top = document.getElementById("talk");
    var close = document.getElementById("close");
    var btn2 = document.getElementById("btn2");
    var btn3 = document.getElementById("btn3");
    var userName = document.getElementById("username");
    var conTent = document.getElementById("content");

function ajaxFirst(currentPage) {
    $.ajax({
        type: 'get',
        url: '/findAllMessage',
        dataType: 'json',
        data:{
            row:'10',
            pageNum:currentPage
        },

        success:function (data) {
            if (data['status'] == 200){
                info(data['result']);
                scrollTo(0,0);
                //分页
                $("#pagination").paging({
                    rows:data['pageInfo']['pageSize'],//每页显示条数
                    pageNum:data['pageInfo']['pageNum'],//当前所在页码
                    pages:data['pageInfo']['pages'],//总页数
                    total:data['pageInfo']['total'],//总记录数
                    callback:function(currentPage){
                        ajaxFirst(currentPage);
                    }
                });
            }else {
                console.log("获取留言失败")
            }
        },
        error:function () {
            console.log("传值失败")
        }
    });
}
   ajaxFirst(1);
    function info(data) {
        var uls = $('#uls');
        uls.append('<div>' +
            '<ul id="insert">' +
            '</ul>' +
            '</div>');
        var insert = $('#insert');
        insert.empty();
        $.each(data,function (index,obj) {
            var newLi = '<li><div class="username">' + '<img class="miniAvatar" src=" '+ obj['avatarUrl'] + '">'+ '<div class="miniNickname">' + obj['username'] + '</div>' +'<p class="delet" >' + obj['sendTime'] + '</p>' +'</div><p>' + obj['message'] + '</p></li>';
            insert.append(newLi)
        });

    }
    btn.onclick = function () {
        reset.style.display = "block";
    };
    close.onclick = function () {
        reset.style.display = "none";
    };
    btn3.onclick = function () {
        reset.style.display = "none";
    };
    btn2.onclick = function () {
        var _content = content.value;

        if (_content == '') {
            alert('内容为空');
            return;
        }
        var sensitiveWords = ['浣犲ソ', '濂戒笉濂�', '涓嶅ソ'];
        sensitiveWords.forEach(function (v) {
            while (_content.indexOf(v) !== -1) {
                _content = _content.replace(v, '***');
            }
        });

        function LimitNumber(txt) {
            var str = txt;
            str = str.substr(0, 150);
            _content.innerText = str;
        }

        if (_content.length > 150) {
            alert("超过字数限制了");
            LimitNumber();
        }else{
            $.ajax({
                type: 'post',
                url: '/saveMessage',
                dataType: 'json',
                data:{
                    message: _content
                },
                success:function (data) {
                    if (data['status'] == 200){
                        alert("成功");
                        window.location.reload();
                    }else {
                        alert("获取数据失败")
                    }
                },
                error:function () {
                    alert("传值失败")
                }
            })


        }

    };
    top.onmousedown = function (ev) {
        var maxLeft = document.documentElement.clientWidth - reset.offsetWidth;
        var maxTop = document.documentElement.clientHeight - reset.offsetHeight;
        var e = ev || window.event;
        var X = e.clientX - reset.offsetLeft;
        var Y = e.clientY - reset.offsetTop;
        if (reset.setCapture) {
            reset.setCapture();
        }
        document.onmousemove = function (ev) {
            var e = ev || window.event;
            var left = e.clientX - X;
            var top = e.clientY - Y;
            if (left < 0) {
                left = 0;
            }
            if (top < 0) {
                top = 0;
            }
            if (top > maxTop) {
                top = maxTop;
            }
            if (left > maxLeft) {
                left = maxLeft;
            }
            reset.style.left = left + 'px';
            reset.style.top = top + 'px';
        };
        document.onmouseup = function () {
            document.onmousemove = null;
            document.onmouseup = null;
            if (reset.releaseCapture) {
                reset.releaseCapture();
            }
        };
    };
};
// var uls = document.getElementById('uls');
// uls.onclick = function (ev) {
//     var e = ev || window.event;
//     var o = e.srcElement || e.target;
//     if (o.nodeName === "A") {
//         uls.removeChild(o.parentNode);
//     }
// };